/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.unifei.portal.dao;

import br.edu.unifei.portal.modelo.Gestor;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Cristiano Affá
 */
public class GestorDao {

    private EntityManager em;

    public GestorDao(EntityManager em) {
        this.em = em;
    }

    public void create(Gestor gestor) {
        em.getTransaction().begin();
        em.persist(gestor);
        em.getTransaction().commit();
    }

    public void update(Gestor gestor) {
        em.getTransaction().begin();
        em.merge(gestor);
        em.getTransaction().commit();
    }

    public void delete(String cpf) {
        em.getTransaction().begin();
        Gestor c = findGestor(cpf);
        em.remove(c);
        em.getTransaction().commit();
    }

    public Gestor findGestor(String cpf) {
        return (Gestor) em.createQuery(
                "from Gestor where cpf = '" + cpf + "'").getSingleResult();
    }

    public List<Gestor> findAllGestor() {
        return em.createQuery("from Gestor").getResultList();
    }

    public List<Gestor> findPageGestor(int max, int first) {
        Query query = em.createQuery("from Gestor");
        query.setMaxResults(max);
        query.setFirstResult(first);
        return query.getResultList();
    }

    public long getCountGestor() {
        Query query = em.createQuery("from Gestor");
        return (Long) em.createQuery(
                "select count(*) from Gestor").getSingleResult();
    }
}
